<?php
namespace Crm\Api;

use PhalApi\Api;
use Crm\Domain\Mall as DomainMall;
use Crm\Domain\WxUser as WxUser;
use Crm\Domain\SysUser as SysUser;
use JiaweiXS\WeApp\WeApp;

/**
 * 平台基础服务类
 *
 * @author: dogstar <chanzonghuang@gmail.com> 2014-10-04
 */

class Site extends Api {

	public function getRules() {
        return array(
            'index' => array(
                'username' 	=> array('name' => 'username', 'default' => '飘渺dk!'),
            ),
            'initialize'=>array('mall_id' => array('name' => 'mall_id', 'type'=>'int','require'=>true,'desc'=>'默认商城ID')),
            'userBook' => array(),
            'userInfoById' => array(
                'esf_id' => array('name' => 'sys_uid', 'type'=>'int', 'require' => true, 'desc' => '用户ID')
            ),
            'qrcodeA' =>array(
                'app_id' => array('name' => 'app_id', 'type'=>'string','require'=>true,'desc'=>'小程序APPID'),
                'path' => array('name' => 'path', 'type'=>'string','require'=>true,'desc'=>'小程序path地址')
            )
        );
	}
	
	/**
	 * 默认接口服务
     * @desc 默认接口服务，当未指定接口服务时执行此接口服务
	 * @return string title 标题
	 * @return string content 内容
	 * @return string version 版本，格式：X.X.X
	 * @return int time 当前时间戳
	 */
	public function index() {
        return array(
            'title' => 'Hello ' . $this->username,
            'version' => PHALAPI_VERSION,
            'time' => $_SERVER['REQUEST_TIME'],
        );
	}

    /**
     * 首页初始化数据
     * @desc 首页初始化数据
     * @return object data 首页初始化数据
     * @return int time 当前时间戳
     */
    public function initialize(){
        $domain = new DomainMall();
        $data['adver'] = $domain->setting($this->mall_id,array('code'=>'2020401','alias'=>'index_adver'));//商城首页广告
        $data['swiper'] = $domain->setting($this->mall_id,array('code'=>'2020401','alias'=>'index_swiper'));//商城首页轮播
        $data['activity'] = $domain->setting($this->mall_id,array('code'=>'2020401','alias'=>'index_activity'));//商城首页活动
        return $data;
    }

    /**
     * 用户通讯录
     * @desc 用户通讯录
     * @return object data 用户通讯录
     * @return int time 当前时间戳
     */
    public function userBook(){
        $domain = new SysUser();
        $rs = $domain->getUserBook();
        foreach ($rs as $key=>$item){
            if($item['org_name']!='运维队') {
                $list[$key]['title'] = $item['org_name'];
                $list[$key]['item'] = $item['userlist'];
            }
        }
        return $list;
    }

    /**
     * 获取职业顾问信息
     * @desc 获取职业顾问信息
     * @return object data 获取职业顾问信息
     * @return int time 当前时间戳
     */
    public function userInfoById(){
        $domain = new SysUser();
        $rs = $domain->getUserInfo($this->esf_id,'id,true_name,mobile,headimgurl');
        return $rs;
    }


    /**
     * 获取小程序A码
     * @desc 获取小程序A码
     * @return object data 获取小程序A码
     * @return int time 当前时间戳
     */
    public function qrcodeA()
    {
        $wxUser = new WxUser();
        $appInfo = $wxUser->getAppInfo($this->app_id);
        //$weapp = new WeApp($appInfo['app_id'],$appInfo['app_secret'],'../runtime/cache/');
        //$weapp = new WeApp('wxa2e507fb874e2ced','45436bdaf4519e049be1dcc7fad999f2','../runtime/cache/');//合一
        $weapp = new WeApp('wx2809752889629206','f5d0be039380b2b9456fbbab13a2653c','../runtime/cache/');//领地
        $qrcode = $weapp->getQRCode();
        $res_array = $qrcode->getQRCodeA($this->path, $width = 120, $auto_color = null, $line_color = null);
        //要生成的图片名字
        $newFilePath = "/Users/luodong/Project/fc114_api/app_fc114/public/images/a.jpg" ;
        if(is_file($newFilePath)){
            return $newFilePath;
        }else{
            $newFile = fopen($newFilePath,"w"); //打开文件准备写入
            fwrite($newFile,$res_array); //写入二进制流到文件
            fclose($newFile); //关闭文件
            return $newFilePath;
        }
    }


}
